<script type="text/ecmascript-6">
  import Navbar from '../../components/base/navbar.vue';
  import { Cache, cache_keys,env,setPageTitle,is } from '../../utils/util';
  import Verify from '../../components/base/verify-code.vue';
  import { quickLogin } from '../../utils/oauth';
  export default{
      name:"coupon_get",
      components:{
          Navbar,
          Verify
      },
      data(){
          return {
            isLogin: false,
            isCheck: false,
            endLoaded: false,
            detail: {},
            message: '',
            num: 1,
            numCN: ''
          }
      },
       methods:{
         getCoupon() {
           if(this.isLogin) {
               this.$store.dispatch("wxGetCoupon");
//             this.wxGetCoupon();
           }else{
//             this.$refs.button.done();
             switch(true) {
               case !is.has(this.mobile):
                   this.$Modal.info({title:"消息提示",body:"请输入您的手机号"});
//                 this.$Alert('请输入您的手机号');
                 break;
               case !is.mobile(this.mobile):
                 this.$Modal.info({title:"消息提示",body:"手机号格式不正确，请重新输入"});
                 break;
               case !is.has(this.code):
                 this.$Modal.info({title:"消息提示",body:"请输入验证码"});
                 break;
               default:
                 quickLogin.call(this, res => {
                   if (res.access_token) {
                     this.$Message.show({text:"登录成功"});
//                     this.$Success('登录成功');
                     this.$store.dispatch("wxGetCoupon");
                   } else {
                     this.$Modal.info({title:"消息提示",body:"验证码不正确"});
                   }
                 });
                 break;
             }
           }
         },
         couponCheck(res) {
           this.detail = res.user;

           if( res.wxBind==1 && res.hasCoupon==1 && res.canGetCoupon==1 ){
             this.num = res.num;
             const cn = ["零","一","两","三","四","五","六","七","八","九","十"];
             if( this.num<=10 ){
               this.numCN = cn[this.num];
             }else{
               this.numCN = this.num;
             }
           }else{
             this.isCheck = true;
             switch (true) {
               case res.wxBind==0:
                 this.message = '仅限微信公众号用户领取!';
                 break;
               case res.hasCoupon==0:
                 this.message = '没有可领取的野练PASS!';
                 break;
               case res.canGetCoupon==0:
                 this.message = '本月已领取过野练PASS!';
                 break;
             }
           }
         },
         gainComplete(res) {
           if(res.status){
             Cache.set(cache_keys.wx_gain_coupon, res.data);
             this.$router.forward({ name: 'gain_coupon', params: {entrance: 'coupon'} });
           }else{
             this.isCheck = true;
             this.message = res.message;
             this.$Modal.info({body:res.message || '领取失败'});
           }
         },
         showResult(){
           this.endLoaded = true;
         }
       },
    beforeRouteLeave(to,from,next){


    },
       created(){
          EventBus.$on("coupon-check",this.couponCheck);
          EventBus.$on("gain-complete",this.gainComplete);
          EventBus.$on("show-result",this.showResult);
         var is_login = !!Cache.get(cache_keys.token);
         if(is_login){
           this.isLogin=true;
         } else{
           if(env.isWechat){
             let type=this.$route.query.type;
             let from=this.$route.query.from;
             let source=this.$route.query.source;
             let openid = this.$route.query.openid || this.$route.query['?openid'];
             if (openid && type === 'oauth') {
               // 查询该openid是否可以登录
               EventBus.$http.post(EventBus.$Config.baseUrl + 'api/oauth/quicklogin', {
                 openid
               }).then(res => {
                 var result = res.data;

                 if (result.access_token) {
                   result.access_token = result.token_type + ' ' + result.access_token;
                   result.expires_in = result.expires_in || 315360000000;  // token不过期
                   result.expires = Date.now() + (result.expires_in - 300) * 1000;

                   Cache.set(cache_keys.token, result, 0);

                   if (result.vipeak_user && result.vipeak_sso) {
                     Cache.cookieStorage.setItem('vipeak_user', result.vipeak_user, {expire: Date.now() + 31536000000});
                     Cache.cookieStorage.setItem('vipeak_sso', result.vipeak_sso, {expire: Date.now() + 31536000000});
                   }
                   this.isLogin = true;
                 } else {
                 }
               }, res => {

               });
             } else {
               // 直接去取openid
               var pathname = window.location.pathname;

               var query = { type: 'oauth' };
               if (from) {
                 query.from = from;
               } else if (source) {
                 query.source = source;
               }
               let url=this.$route.fullPath;
               if ( this.$router.mode === 'hash' ) url = '#!' + url;
               if (from) {
                 url+='?from='+from;
               }
               else{
                 url+='?source='+source;
               }
//                 let url = this.$router.stringifyPath({ name: 'get_coupon', query });
//                 if ( this.$router.mode === 'hash' ) url = '#!' + url;
//                 console.log(url);
               url = decodeURIComponent(url);

               url = EventBus.$Config.baseUrl + 'oauth/wxlogin?redirect_url=' + encodeURIComponent(window.location.origin + pathname + url + '&');

               window.location.href = url;
             }
           }
           else{

           }

         }
       },
       beforeDestroy(){
         EventBus.$off("coupon-check");
         EventBus.$off("gain-complete");
         EventBus.$off("show-result");
       },
       mounted(){
           if(this.isLogin){
               this.$store.dispatch('queryCoupon');
           }else{
               this.endLoaded =true;
           }
       }
  }
</script>
